package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.davemorrissey.labs.subscaleview.ImageSource;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityLongSpliceBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.RxUtil;

public class LongSpliceActivity extends BaseAc<ActivityLongSpliceBinding> {

    public static List<String> listPath = new ArrayList<>();
    protected boolean isPicTooLarge = false;//判断图片是否过大
    private Bitmap OriBitmap;//横竖图的bitmap
    private boolean isOri = true;

    @Override
    protected int onCreate() {
        return R.layout.activity_long_splice;
    }

    @Override
    protected void initView() {
        mDataBinding.ivLongSpliceBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvLongSpliceSave.setOnClickListener(this);
        mDataBinding.llChange.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvLongSpliceSave://保存
                if (OriBitmap == null) return;
                saveImg();
                break;
            case R.id.llChange://横竖切换
                isOri = !isOri;
                mDataBinding.tvChange.setText(isOri ? getString(R.string.splice_switch_text1) : getString(R.string.splice_switch_text2));
                doPinTu(isOri);
                break;
        }
    }

    @Override
    protected void initData() {
        if (listPath.size() == 0) return;
        doPinTu(isOri);
    }

    //保存图片
    private void saveImg() {
        showDialog(getString(R.string.saving));
        RxUtil.create(new RxUtil.Callback<String>() {
            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                String resultFile = FileUtil.generateFilePath(Content.myCreate, ".png");
                ImageUtils.save2Album(OriBitmap, Bitmap.CompressFormat.PNG);
                ImageUtils.save(OriBitmap, resultFile, Bitmap.CompressFormat.PNG);
                emitter.onNext(resultFile);
            }

            @Override
            public void accept(String path) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        dismissDialog();
                        LongSpliceResultActivity.imgPath = path;
                        startActivity(LongSpliceResultActivity.class);
                    }
                }, 500);
            }
        });
    }

    //设置图片横竖
    protected void doPinTu(final boolean vertical) {
        showDialog(getString(com.stark.common.res.R.string.handling));
        RxUtil.create(new RxUtil.Callback<Bitmap>() {
            @Override
            public void doBackground(ObservableEmitter<Bitmap> emitter) {
                isPicTooLarge = false;
                Bitmap bitmap1 = null;
                try {
                    bitmap1 = BitmapFactory.decodeFile(listPath.get(0));
                    bitmap1 = ImageUtils.rotate(bitmap1, ImageUtils.getRotateDegree(listPath.get(0)),
                            bitmap1.getWidth() / 2, bitmap1.getHeight() / 2, true);
                    for (int i = 1; i < listPath.size(); i++) {
                        Bitmap bitmap2 = BitmapFactory.decodeFile(listPath.get(i));
                        bitmap2 = ImageUtils.rotate(bitmap2, ImageUtils.getRotateDegree(listPath.get(i)),
                                bitmap2.getWidth() / 2, bitmap2.getHeight() / 2, true);
                        if (!vertical) {
                            bitmap1 = horizontalPintu(bitmap1, bitmap2);
                        } else {
                            bitmap1 = pintu(bitmap1, bitmap2);
                        }
                    }
                } catch (OutOfMemoryError e) {
                    //e.printStackTrace();
                    isPicTooLarge = true;
                }
                emitter.onNext(bitmap1);
            }

            @Override
            public void accept(Bitmap bitmap) {
                dismissDialog();
                if (isPicTooLarge) {
                    ToastUtils.showShort(R.string.pic_is_too_large);
                }
                if (bitmap != null) {
                    mDataBinding.mySubImgView.setImage(ImageSource.bitmap(bitmap));
                }
                //获取要保存的图片bitmap
                OriBitmap = bitmap;
            }
        });
    }

    //竖向
    private Bitmap pintu(Bitmap bitmap1, Bitmap bitmap2) {
        Bitmap ret;
        int width = bitmap1.getWidth();
        if (width > 1024) {
            int h1 = bitmap1.getHeight() * 1024 / bitmap1.getWidth();
            bitmap1 = ImageUtils.scale(bitmap1, 1024, h1, true);
            width = 1024;
        }
        if (bitmap2.getWidth() != width) {
            int h2 = bitmap2.getHeight() * width / bitmap2.getWidth();
            ret = Bitmap.createBitmap(width, bitmap1.getHeight() + h2, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(ret);
            Bitmap newSizeBitmap2 = ImageUtils.scale(bitmap2, width, h2, true);
            canvas.drawBitmap(bitmap1, 0, 0, null);
            canvas.drawBitmap(newSizeBitmap2, 0, bitmap1.getHeight(), null);
        } else {
            ret = Bitmap.createBitmap(width, bitmap1.getHeight() + bitmap2.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(ret);
            canvas.drawBitmap(bitmap1, 0, 0, null);
            canvas.drawBitmap(bitmap2, 0, bitmap1.getHeight(), null);
        }
        return ret;
    }

    //横向
    private Bitmap horizontalPintu(Bitmap bitmap1, Bitmap bitmap2) {
        Bitmap ret;
        int height = bitmap1.getHeight();
        if (height > 1024) {
            int w1 = bitmap1.getWidth() * 1024 / bitmap1.getHeight();
            bitmap1 = ImageUtils.scale(bitmap1, w1, 1024, true);
            height = 1024;
        }
        if (bitmap2.getHeight() != height) {
            int w2 = bitmap2.getWidth() * height / bitmap2.getHeight();
            ret = Bitmap.createBitmap(bitmap1.getWidth() + w2, height, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(ret);
            Bitmap newSizeBitmap2 = ImageUtils.scale(bitmap2, w2, height, true);
            canvas.drawBitmap(bitmap1, 0, 0, null);
            canvas.drawBitmap(newSizeBitmap2, bitmap1.getWidth(), 0, null);
        } else {
            ret = Bitmap.createBitmap(height, bitmap1.getWidth() + bitmap2.getWidth(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(ret);
            canvas.drawBitmap(bitmap1, 0, 0, null);
            canvas.drawBitmap(bitmap2, bitmap1.getWidth(), 0, null);
        }
        return ret;
    }

}